package com.helloworld.demo;

import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Method;

public class MyClassLoaderTest extends ClassLoader {

    private String classPath;

    public MyClassLoaderTest(String classPath) {
        this.classPath = classPath;
    }

    private byte[] loadByte(String name) throws IOException {
        name = name.replaceAll("\\.", "/");
        FileInputStream fileInputStream = new FileInputStream(classPath + name + ".class");
        int len = fileInputStream.available();
        byte[] bytes = new byte[len];
        fileInputStream.read(bytes);
        fileInputStream.close();
        return bytes;
    }

    protected Class<?> findClass(String name) throws ClassNotFoundException {
        byte[] data = new byte[0];
        try {
            data = loadByte(name);
            //将Class的字节数组转换成Class对象
            return defineClass(name, data, 0, data.length);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    protected Class<?> loadClass(String name, boolean resolve)
            throws ClassNotFoundException
    {
        synchronized (getClassLoadingLock(name)) {
            // First, check if the class has already been loaded
            Class<?> c = findLoadedClass(name);
            if (c == null) {
                long t0 = System.nanoTime();
                // If still not found, then invoke findClass in order
                // to find the class.
                long t1 = System.nanoTime();

                if(name.equals("com.helloworld.demo.User")){
                    c = findClass(name);
                }else{
                    c = this.getParent().loadClass(name);
                }



                // this is the defining class loader; record the stats
                sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
                sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
                sun.misc.PerfCounter.getFindClasses().increment();
            }
            if (resolve) {
                resolveClass(c);
            }
            return c;
        }
    }

    public static void main(String[] args) throws Exception {
        MyClassLoaderTest myClassLoaderTest = new MyClassLoaderTest("E:\\helloworldWorkSpace\\Temp\\");
        Class<?> clazz = myClassLoaderTest.loadClass("com.helloworld.demo.User");
        Object o = clazz.newInstance();
        Method declaredMethod = clazz.getDeclaredMethod("print", null);
        declaredMethod.invoke(o,null);

        System.out.println(clazz.getClassLoader());

        MyClassLoaderTest myClassLoaderTest1 = new MyClassLoaderTest("E:\\helloworldWorkSpace\\Temp1\\");
        Class<?> clazz1 = myClassLoaderTest1.loadClass("com.helloworld.demo.User");
        Object o1 = clazz1.newInstance();
        Method declaredMethod1 = clazz1.getDeclaredMethod("print", null);
        declaredMethod1.invoke(o1,null);

        System.out.println(clazz1.getClassLoader());
    }
}
